#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   统一诊断服务测试_app_test.py
@Time    :   2025/03/20 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangl@genvict.com
@License :   (C)Copyright 2022-2025
@Desc    :   统一诊断服务测试APP
'''

from pandas._libs import interval
import pytest
import allure
import time
from pytest_assume.plugin import assume
import Set_up
from commDriver.TS_CAN import *
import math


class Test_UnifiedDiagnosticService_APP:
    
    @allure.severity('critical')
    @allure.title('在应用程序的默认会话中，电控单元须支持物理寻址和功能寻址的$19服务')
    @allure.description('在应用程序的默认会话中，电控单元须支持物理寻址和功能寻址的$19服务')
    def test_19_Default(self, action, Driver):
        iStep = 1
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "10 01", "50 01 00 32 01 f4", 0, iStep, False, 'UDS')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 01 FF", "59 01", 0, iStep, False, 'UDS')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 01 FF", "59 01", 0, iStep, False, 'UDSFun')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 02 FF", "59 02", 0, iStep, False, 'UDS')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 02 FF", "59 02", 0, iStep, False, 'UDSFun')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 04 91 17 16 01", "59 04", 0, iStep, False, 'UDS')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 04 91 17 16 01", "59 04", 0, iStep, False, 'UDSFun')


    
    @allure.severity('critical')
    @allure.title('在应用程序的扩展会话中，电控单元须支持物理寻址和功能寻址的$19服务')
    @allure.description('在应用程序的扩展会话中，电控单元须支持物理寻址和功能寻址的$19服务')
    def test_19_Extend(self, action, Driver):
        iStep = 1
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "10 03", "50 03 00 32 01 f4", 0, iStep, False, 'UDS')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 01 FF", "59 01", 0, iStep, False, 'UDS')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 01 FF", "59 01", 0, iStep, False, 'UDSFun')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 02 FF", "59 02", 0, iStep, False, 'UDS')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 02 FF", "59 02", 0, iStep, False, 'UDSFun')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 04 91 17 16 01", "59 04", 0, iStep, False, 'UDS')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 04 91 17 16 01", "59 04", 0, iStep, False, 'UDSFun')

    @allure.severity('critical')
    @allure.title('选择快照记录编号为$01，发送读取诊断故障代码快照请求，检查电控单元反馈对应诊断故障代码的全局快照数据')
    @allure.description('选择快照记录编号为$01，发送读取诊断故障代码快照请求，检查电控单元反馈对应诊断故障代码的全局快照数据')
    def test_1904_Snapshot_01(self, action, Driver):
        iStep = 1
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "10 01", "50 01 00 32 01 f4", 0, iStep, False, 'UDS')
        # 设置电压为8v,生成低压故障
        iStep = Driver['CAN'].SetVolt(Driver, 8.3, 3, iStep)
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 04 91 17 16 01", "59 04 91 17 16 09 01", 0, iStep, False, 'UDS')
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)

    @allure.severity('critical')
    @allure.title('在应用程序的默认会话中，电控单元支持物理寻址，不支持功能寻址的读取诊断故障代码信息服务$06子功能，且响应格式与诊断规范一致')
    @allure.description('在应用程序的默认会话中，电控单元支持物理寻址，不支持功能寻址的读取诊断故障代码信息服务$06子功能，且响应格式与诊断规范一致')
    def test_1906_01_Default(self, action, Driver):
        iStep = 1
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "10 01", "50 01 00 32 01 f4", 0, iStep, False, 'UDS')
        # 设置电压为8v,生成低压故障
        iStep = Driver['CAN'].SetVolt(Driver, 8.3, 5, iStep)
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 06 91 17 16 01", "59 06 91 17 16 09 01", 0, iStep, False, 'UDS')
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)

    @allure.severity('critical')
    @allure.title('在应用程序的扩展会话中，电控单元支持物理寻址，不支持功能寻址的读取诊断故障代码信息服务$06子功能，且响应格式与诊断规范一致')
    @allure.description('在应用程序的扩展会话中，电控单元支持物理寻址，不支持功能寻址的读取诊断故障代码信息服务$06子功能，且响应格式与诊断规范一致')
    def test_1906_01_Extend(self, action, Driver):
        iStep = 1
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "10 03", "50 03 00 32 01 f4", 0, iStep, False, 'UDS')
        # 设置电压为8v,生成低压故障
        iStep = Driver['CAN'].SetVolt(Driver, 8.3, 7, iStep)
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 06 91 17 16 01", "59 06 91 17 16 09 01", 0, iStep, False, 'UDS')
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)

    @allure.severity('critical')
    @allure.title('发送长度不正确的诊断请求$19,电控单元须反馈否定应答代码0x12')
    @allure.description('')
    def test_19_NRC12_Default(self, action, Driver):
        iStep = 1
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "10 01", "50 01 00 32 01 f4", 0, iStep, False, 'UDS')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 1A", "7F 19 12", 0, iStep, False, 'UDS')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 1B", "", 0, iStep, False, 'UDSFun')


    @allure.severity('critical')
    @allure.title('发送长度不正确的诊断请求$19,电控单元须反馈否定应答代码0x13')
    @allure.description('发送长度不正确的诊断请求$19,电控单元须反馈否定应答代码0x13')
    def test_19_NRC13_Default(self, action, Driver):
        iStep = 1
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "10 01", "50 01 00 32 01 f4", 0, iStep, False, 'UDS')

        iStep, resp, _ = Driver['CAN'].Send_And_Receive(Driver, "03 19 0A 00 00 00 00 00", iStep)
        with allure.step(f"第{iStep}步:检查响应消息内容 # 7F 19 13"):
            assert resp[:4] == [0x03, 0x7F, 0x19, 0x13], logging.error(f"响应消息内容错误, 实际响应消息内容: {resp}")

        iStep, resp, _ = Driver['CAN'].Send_And_Receive(Driver, "01 19 02 00 00 00 00 00", iStep)
        with allure.step(f"第{iStep}步:检查响应消息内容 # 7F 19 13"):
            assert resp[:4] == [0x03, 0x7F, 0x19, 0x13], logging.error(f"响应消息内容错误, 实际响应消息内容: {resp}")

    @allure.severity('critical')
    @allure.title('在应用程序的扩展会话中，电控单元须支持物理寻址和功能寻址的$19服务的NRC31')
    @allure.description('在应用程序的扩展会话中，电控单元须支持物理寻址和功能寻址的$19服务的NRC31')
    def test_19_NRC31_Phy(self, action, Driver):
        iStep = 1
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "10 01", "50 01 00 32 01 f4", 0, iStep, False, 'UDS')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 04 AA BB CC DD", "7F 19 31", 0, iStep, False, 'UDS')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 04 99 88 77 01", "7F 19 31", 0, iStep, False, 'UDS')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 04 91 17 16 03", "7F 19 31", 0, iStep, False, 'UDS')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "19 06 91 17 16 03", "7F 19 31", 0, iStep, False, 'UDS')

    
    @allure.severity('critical')
    @allure.title('发送长度不正确的诊断请求$31,电控单元须反馈否定应答代码0x13')
    @allure.description('发送长度不正确的诊断请求$31,电控单元须反馈否定应答代码0x13')
    def test_31_NRC13_Extended(self, action, Driver):
        iStep = 1
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, "10 03", "50 03 00 32 01 f4", 0, iStep, False, 'UDS')

        iStep, resp, _ = Driver['CAN'].Send_And_Receive(Driver, "01 31 01 02 03 00 00 00", iStep)
        with allure.step(f"第{iStep}步:检查响应消息内容 # 7F 31 13"):
            assert resp[:4] == [0x03, 0x7F, 0x31, 0x13], logging.error(f"响应消息内容错误, 实际响应消息内容: {resp}")

        iStep, resp, _ = Driver['CAN'].Send_And_Receive(Driver, "05 31 01 02 03 00 00 00", iStep)
        with allure.step(f"第{iStep}步:检查响应消息内容 # 7F 31 13"):
            assert resp[:4] == [0x03, 0x7F, 0x31, 0x13], logging.error(f"响应消息内容错误, 实际响应消息内容: {resp}")
